

#include "stdafx.h"
#include "pf_preview.h"

#include "pf_entitycontainer.h"
#include "pf_graphicview.h"
#include "pf_information.h"
#include "pf_mouseevent.h"
#include "pf_settings.h"


/**
 * Constructor.
 */
PF_Preview::PF_Preview(PF_Container* parent)
        : PF_Container(parent) {

    PF_SETTINGS->beginGroup("/Appearance");
    maxEntities = PF_SETTINGS->readNumEntry("/MaxPreview", 100);
    PF_SETTINGS->endGroup();
}



/**
 * Destructor.
 */
PF_Preview::~PF_Preview() {
    /*
    PF_SETTINGS->beginGroup("/Appearance");
    PF_SETTINGS->writeEntry("/MaxPreview", maxEntities);
    PF_SETTINGS->endGroup();
    */
}



/**
 * Adds an entity to this preview and removes any attributes / layer
 * connectsions before that.
 */
void PF_Preview::addEntity(PF_Entity* entity) {
    if (entity==NULL || entity->isUndone()) {
        return;
    }
	

    // only border preview for complex entities:
    //if ((entity->count() > maxEntities-count()) &&

    bool addBorder = false;

    if (entity->rtti()==LX_Define::EntityImage || entity->rtti()==LX_Define::EntityHatch || 
		entity->rtti()==LX_Define::EntityInsert) {

        addBorder = true;
    } else {
        if (entity->isContainer() && entity->rtti()!=LX_Define::EntitySpline) {
            if (entity->countDeep() > maxEntities-countDeep()) {
                addBorder = true;
            }
        }
    }

    if (addBorder) {
        PF_Vector min = entity->getMin();
        PF_Vector max = entity->getMax();

        PF_Line* l1 =
            new PF_Line(this,
                        PF_LineAttribute(PF_Vector(min.x, min.y),
                                    PF_Vector(max.x, min.y)));
        PF_Line* l2 =
            new PF_Line(this,
                        PF_LineAttribute(PF_Vector(max.x, min.y),
                                    PF_Vector(max.x, max.y)));
        PF_Line* l3 =
            new PF_Line(this,
                        PF_LineAttribute(PF_Vector(max.x, max.y),
                                    PF_Vector(min.x, max.y)));
        PF_Line* l4 =
            new PF_Line(this,
                        PF_LineAttribute(PF_Vector(min.x, max.y),
                                    PF_Vector(min.x, min.y)));

        PF_Container::addEntity(l1);
        PF_Container::addEntity(l2);
        PF_Container::addEntity(l3);
        PF_Container::addEntity(l4);

        delete entity;
        entity = NULL;
    } else {
        entity->setLayer(NULL);
        entity->setSelected(false);
        entity->reparent(this);
		// Don't set this pen, let drawing routines decide entity->setPenToActive();
        PF_Container::addEntity(entity);
    }
}



/**
 * Clones the given entity and adds the clone to the preview.
 */
void PF_Preview::addCloneOf(PF_Entity* entity) {
    if (entity==NULL) {
        return;
    }

    PF_Entity* clone = entity->clone();
    addEntity(clone);
}



/**
 * Adds all entities from 'container' to the preview (unselected).
 */
void PF_Preview::addAllFrom(PF_Container& container) {
    int c=0;
    for (PF_Entity* e=container.firstEntity();
            e!=NULL; e=container.nextEntity()) {

        if (c<maxEntities) {
            PF_Entity* clone = e->clone();
            clone->setSelected(false);
            clone->reparent(this);

            c+=clone->countDeep();
            addEntity(clone);
            // clone might be NULL after this point
        }
    }
}


/**
 * Adds all selected entities from 'container' to the preview (unselected).
 */
void PF_Preview::addSelectionFrom(PF_Container& container) {
    int c=0;
    for (PF_Entity* e=container.firstEntity();
            e!=NULL; e=container.nextEntity()) {

        if (e->isSelected() && c<maxEntities) {
            PF_Entity* clone = e->clone();
            clone->setSelected(false);
            clone->reparent(this);

            c+=clone->countDeep();
            addEntity(clone);
            // clone might be NULL after this point
        }
    }
}


/**
 * Adds all entities in the given range and those which have endpoints
 * in the given range to the preview.
 */
void PF_Preview::addStretchablesFrom(PF_Container& container,
                                     const PF_Vector& v1, const PF_Vector& v2) {

    int c=0;

    for (PF_Entity* e=container.firstEntity();
            e!=NULL; e=container.nextEntity()) {

        if (e->isVisible() &&
                e->rtti()!=LX_Define::EntityHatch &&
                ((e->isInWindow(v1, v2)) ||
                 e->hasEndpointsWithinWindow(v1, v2)) &&

                c<maxEntities) {

            PF_Entity* clone = e->clone();
            //clone->setSelected(false);
            clone->reparent(this);

            c+=clone->countDeep();
            addEntity(clone);
            // clone might be NULL after this point
        }
    }
}


